// 入口层

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

global.path = require('path');
global.basename = __dirname;
global.fs = require("fs");
//引入multer中间件
global.multer = require("multer");
//设置文件上传的保存地址
global.upload = multer({dest:__dirname+"/public/images"})

//引入拦截层
const middleware = require(path.resolve(basename, 'middleware/middleware.js'));
middleware(app);

// 解析 application/json
app.use(express.json());
// 解析 application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false, limit: 20971520 }));

//使用静态资源
app.use('/static', express.static(path.join(__dirname, 'public')));

// app.use(bodyParser({
//     limit: 20971520  
// }));

// 引入jsonwebtoken
global.jwt = require('jsonwebtoken');

//引入配置层
const { serverOptions } = require(path.resolve(basename, 'config/config.js'))

//引入路由层
const routes = require(path.resolve(basename, 'routes/routes.js'))
routes(app);

//引入控制器层
global.controller = require(path.resolve(basename, 'controller/controller.js'));

// 引入mongoose
global.mongoose = require('mongoose');

// 连接本地数据库
global.db = require(path.resolve(basename, 'db/connect.js'));

//引入全局model
global.model = require(path.resolve(basename, 'db/models/model.js'));

app.listen(serverOptions.port, function (req, res) {
    console.log(`服务开启成功　${serverOptions.host}:${serverOptions.port}`);
})